﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.BusiTravel;
using Enterprise.Data.BusiTravel;
using Enterprise.Infrastructure;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
using Enterprise.Service.Rtx;
namespace Enterprise.Service.BusiTravel
{
    /// <summary>
    /// 差旅管理
    /// </summary>
    public class BusiTravelService
    {
        /// <summary>
        /// 缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-BusiTravel";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<BusiTravelEntity> BusiTravelList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<BusiTravelEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                BusiTravelData data = new BusiTravelData();
                List<BusiTravelEntity> list = data.BusiTravelList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回记录
        /// </summary>
        /// <param name="odId"></param>
        /// <returns></returns>
        public BusiTravelEntity BusiTravelDisp(string bId)
        {
            return BusiTravelList().Where(p => p.BID == bId).FirstOrDefault();
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void BusiTravelExecute(BusiTravelEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into BUSITRAVEL_INFO(BID,DEPTID,USERID,BSUBJECT,BDESTINATION,BLINE,BTICKETTYPE,BSTARTIME,BENDTIME,BCHECKERS,BSTATE,BCREATETIME) values('" + entity.BID + "'," + entity.DEPTID + "," + entity.USERID + ",'" + entity.BSUBJECT + "','" + entity.BDESTINATION + "','" + entity.BLINE + "','" + entity.BTICKETTYPE + "'," + Utility.FormatOracleDate(entity.BSTARTIME) + "," + Utility.FormatOracleDate(entity.BENDTIME) + ",'" + entity.BCHECKERS + "'," + entity.BSTATE + "," + Utility.FormatOracleDate(entity.BCREATETIME) + ")";
                    break;
                case "Close":
                    cmd = "Update BUSITRAVEL_INFO set BENDTIME=" + Utility.FormatOracleDate(entity.BENDTIME) + " ,BCLOSE=" + entity.BCLOSE + ",BCLOSETIME=" + Utility.FormatOracleDate(entity.BCLOSETIME) + " where BID='" + entity.BID + "'";
                    break;
                case "Delete":
                    cmd = "Delete from BUSITRAVEL_INFO where BID='" + entity.BID + "'";
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                BusiTravelExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void BusiTravelExecuteBySql(string cmd, int typeId)
        {
            BusiTravelData data = new BusiTravelData();
            data.Execute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        /// <summary>
        /// 返回所有待用户审核的公文申请单
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<WorkFlowProcessEntity> BusiTravelUnCheck(int UserId)
        {
            WorkFlowProcessService wfService = new WorkFlowProcessService();
            return wfService.WorkFlowProcessList().Where(p => p.PISCHECK == 0 && p.PUSERID == UserId && p.PWFTYPE == 21).ToList();
        }

        /// <summary>
        /// 操作方法语句
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="ho"></param>
        public void Notice(BusiTravelEntity ho, int iIndex,int wfTypeId)
        {
            string[] s = ho.BCHECKERS.Split(',');
            if (s.ToList().Count > 0)
            {
                if (s.ToList().Count > iIndex)
                {
                    int checkUserId = Convert.ToInt32(s.ToList()[iIndex]);
                    int userId = checkUserId;
                    //判断是否存在授权设置
                    UserWfConfigService cfService = new UserWfConfigService();
                    var query = cfService.UserWfConfig(Utility.Get_UserId).Where(p => p.WFTYPEID == wfTypeId && p.ENDTIME > DateTime.Now).FirstOrDefault();
                    if (query != null && query.SQUSERID != 0)
                    {
                        userId = query.SQUSERID;
                    }
                    WorkFlowProcessService wfpService = new WorkFlowProcessService();
                    wfpService.WorkFlowProcessInsertNew(ho.BID, checkUserId, userId, wfTypeId, iIndex + 1);
                    #region "发送Rtx消息"
                    string sUserName = Enterprise.Service.User.UserService.GetUserName(ho.USERID);
                    string sTitle = string.Format("审批流程[出差申请(员工:{0})]", sUserName);
                    string sContent = sTitle + ",请登录平台完成出差申请";
                    RtxService.SendRtxMessageServices(userId, sTitle, sContent);
                    #endregion
                }
                else if (s.ToList().Count == iIndex)
                {
                    ho.BSTATE = 1;
                    string cmd = "Update BUSITRAVEL_INFO set BSTATE=" + ho.BSTATE + " where BID='" + ho.BID + "'";
                    BusiTravelExecuteBySql(cmd, 1);
                    string sTitle = string.Format("审批流程[您的出差申请单({0})]", ho.BSUBJECT);
                    string sContent = sTitle + string.Format(",已经通过审批流程!");
                    RtxService.SendRtxMessageServices(ho.USERID, sTitle, sContent);
                }
            }
        }
    }
}
